package com.michaelbulava.TFS.ui.wizards.common.connect;

import com.intellij.ide.wizard.CommitStepException;
import com.intellij.ui.components.JBScrollPane;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.michaelbulava.TFS.Messages;
import com.michaelbulava.TFS.ui.controls.connect.ConnectionErrorControl;
import com.michaelbulava.TFS.ui.controls.connect.TeamProjectSelectControl;
import com.michaelbulava.TFS.ui.controls.vc.ServerItemTreeControl;
import com.michaelbulava.TFS.ui.framework.wizard.ExtendedWizardStep;
import com.michaelbulava.TFS.ui.wizards.common.IConnectWizard;
import com.microsoft.tfs.core.TFSTeamProjectCollection;
import com.microsoft.tfs.core.clients.commonstructure.ProjectInfo;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;

/**
 * Created by mbulava on 6/22/2015.
 */
public class SelectServerAndProjectsWizardStep extends ExtendedWizardStep{
    public static final Object STEP_ID = new Object();

    private final IConnectWizard parentWizard;
    private final boolean requireTeamProjectSelection;

    private JPanel control;
    private TeamProjectSelectControl teamProjectSelectControl;
    private ConnectionErrorControl errorControl;


    public SelectServerAndProjectsWizardStep(IConnectWizard parentWizard, boolean requireTeamProjectSelection){
        super(Messages.getString("SelectServerAndProjectsWizardStep.Title"));
        this.parentWizard = parentWizard;
        this.requireTeamProjectSelection = requireTeamProjectSelection;

        initControl();
    }

    private void initControl(){
        control = new JPanel();
        teamProjectSelectControl = new TeamProjectSelectControl();
        teamProjectSelectControl.setProject(this.parentWizard.getProject());

        teamProjectSelectControl.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                fireStateChanged();
            }
        });
        errorControl = new ConnectionErrorControl();

        control.setLayout(new GridLayoutManager(2, 1, new Insets(4, 4, 4, 4), -1, -1));
        errorControl.setVisible(false);
        control.add(errorControl, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_NORTHWEST,
                GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
        control.add(teamProjectSelectControl, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER,
                GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
                GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));

//        if (parentWizard.getWizardData(TFSTeamProjectCollection.class.getName()) != null){
//            TFSTeamProjectCollection collection = (TFSTeamProjectCollection) parentWizard
//                    .getWizardData(TFSTeamProjectCollection.class.getName());
//            teamProjectSelectControl.setSelectedConnectionURI(collection.getConfigurationServer().getBaseURI());
//            teamProjectSelectControl.setSelectedCollection(collection);
//        }
    }

    @Override
    public void _init(){
        if (parentWizard.getTeamProjectCollection() != null){
            TFSTeamProjectCollection collection = (TFSTeamProjectCollection) parentWizard.getTeamProjectCollection();
            if (teamProjectSelectControl.getTeamProjectCollection() != collection){
                teamProjectSelectControl.setSelectedConnectionURI(collection.getConfigurationServer().getBaseURI());
                teamProjectSelectControl.setSelectedCollection(collection);
            }
        }
        if (parentWizard.getTeamProjects() != null){
            ProjectInfo[] projects = (ProjectInfo[]) parentWizard.getTeamProjects();
            if (projects.length > 0){
                teamProjectSelectControl.setSelectedProjects(projects);
            }
        }
    }

    @Override
    public Object getStepId() {
        return STEP_ID;
    }

    @Override
    public Object getNextStepId() {
        return parentWizard.getNextStepId(this);
    }

    @Override
    public Object getPreviousStepId() {
        return parentWizard.getPreviousStepId(this);
    }

    @Override
    public void commit(CommitType commitType) throws CommitStepException {
        if (commitType == CommitType.Next | commitType == CommitType.Finish) {
            this.parentWizard.setTeamProjectCollection(teamProjectSelectControl.getTeamProjectCollection());
            if (this.teamProjectSelectControl.getSelectedTeamProjects().length > 0)
                parentWizard.setTeamProjects(teamProjectSelectControl.getSelectedTeamProjects());
            else
                parentWizard.setTeamProjects(null);
        }
    }

    @Override
    public boolean isComplete() {
        return ((this.teamProjectSelectControl.getTeamProjectCollection() != null) &&
                ((this.requireTeamProjectSelection &&
                        this.teamProjectSelectControl.getSelectedTeamProjects().length > 0) ||
                        !this.requireTeamProjectSelection));
    }

    @Override
    public JComponent getComponent() {
        return control;
    }

    @Override
    public JComponent getPreferredFocusedComponent() {
        return null;
    }

}
